﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Wusong.Gold.DataService;

namespace GoldReader
{
    public partial class Service : ServiceBase
    {
        private int counter = 0;
        private IList<IWriterBase> writList = new List<IWriterBase>();

        public Service()
        {
            InitializeComponent();
            ServiceName = "GoldReader";
        }

        protected override void OnStart(string[] args)
        {
            IWriterBase write = new PlainTextWriter(null);
            IWriterBase dataWrite = new DataBaseWriter(null);
            writList.Add(write);
            writList.Add(dataWrite);
            ZJWResource resource = new ZJWResource();
            using (ReaderBase reader = new ReaderBase(resource))
            {
                Console.WriteLine("start read");
                Console.WriteLine("press q to quit");
                reader.AfterRead += OnAfterRead;
                reader.Start();
                while (Console.Read() != 'q') //直到按小写字母q退出,否则一直抓取下去
                {

                }
            }
        }

        protected override void OnStop()
        {
        }

        private void OnAfterRead(object sender, ReadEventArgs args)
        {
            if (writList != null && writList.Count > 0)
            {
                foreach (IWriterBase writer in writList)
                {
                    writer.Write(args.Value);
                }
            }
        }

    }
}
